package sibu.cn.mydemo.annotation;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.view.View;

import java.lang.reflect.Field;

/**
 * Created by 刘红亮 on 15-11-5 上午11:38.
 */
public class AnnotateUtil {

    public AnnotateUtil() {

    }

    public static void initBindView(Object currentClass, View sourceView) {

        Field[] fields = currentClass.getClass().getDeclaredFields();

        if (fields != null && fields.length > 0) {
            Field[] var6 = fields;
            int var5 = fields.length;

            for (int var4 = 0; var4 < var5; var4++) {
                Field field = var6[var4];
                BindView bindView = field.getAnnotation(BindView.class);
                if (bindView != null) {
                    int viewId = bindView.id();
                    boolean clickLis = bindView.click();
                    try {

                        field.setAccessible(true);
                        if(clickLis){
                            sourceView.findViewById(viewId).setOnClickListener((View.OnClickListener)currentClass);
                        }
                        field.set(currentClass,sourceView.findViewById(viewId));
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void initBindView(Activity aty){
        initBindView(aty,aty.getWindow().getDecorView());
    }

    public static void initBindView(View view){
        Context context = view.getContext();

        if(context instanceof Activity){
            initBindView((Activity)context);
        }else {

        }
    }

    public static void initBindView(Fragment frg){
        initBindView(frg,frg.getActivity().getWindow().getDecorView());
    }
}
